// Device/pages/Analysis/index.js
/**
 * 设备详情 - 分析页 
 * 血压计 & 血糖仪
 * 
 * create & edit by cyj at 2024-09-10
 * */
import {
    fetchDeviceInfo
} from '@/services/device';
import { getParams } from '@/utils/util'
const componentMappings = {
    8: 'pressure-component',
    9: 'sugar-component'
}

Page({

    /**
     * 页面的初始数据
     */
    data: {
        user_options: [{
                text: '本人',
                value: '本人'
            },
            {
                text: '配偶',
                value: '配偶'
            },
            {
                text: '子女',
                value: '子女'
            },
            {
                text: '其他',
                value: '其他'
            },
        ],
        user_tag: '本人',
        config: {},
        currentComponent: null,
        deviceInfo: {},
        params: {}
    },
    /**
     * 生命周期函数--监听页面加载
     */
    async onLoad(options) {
        const { device_imei } = getParams(options.params);
        const res = await fetchDeviceInfo({
            device_imei
        });
        this.setData({
            deviceInfo: res.data,
            currentComponent: componentMappings[res.data.device_type],
            params: {
                ...this.data.params,
                device_imei: res.data.device_imei,
                user_tag: this.data.user_tag,
                currentIndex: 0
            }
        })
        this.setData({
            config: {
                tooltip: {
                    show: true,
                    confine: true,
                    trigger: 'item',
                    triggerOn: 'click'
                },
                xAxis: {
                    type: 'category',
                    data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
                },
                yAxis: {
                    type: 'value'
                },
                series: [{
                    data: [150, 230, 224, 218, 135, 147, 260],
                    type: 'line'
                }]
            }
        })
    },
    onDateChange(e) {
        let params = this.data.params;
        switch (e.detail.filterType) {
            case 'day':
                params.currentIndex = 0;
                params.stime = e.detail.startDate;
                break;
            case 'week':
                params.currentIndex = 1;
                params.stime = e.detail.startDate;
                params.otime = e.detail.endDate;
                break;
            case 'month':
                params.currentIndex = 2;
                params.stime = e.detail.startDate;
                params.otime = e.detail.endDate;
                break;
            default:
                break;
        }
        this.setData({
            params
        })
    },
    onDropChange(e) {
        this.setData({
            params: {
                ...this.data.params,
                user_tag: e.detail
            }
        })
    }
})